<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'当前商品列表'),
	array('op'=>'otherlist','title'=>'其他场景商品列表'),
	array('op'=>'edit','title'=>'添加商品_ADD Goods'),
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>4,'nochecked'=>0,'no_showsys'=>0);
	$params['status']='1';
	if(!empty($_RQ['title'])){
		$params['title OR']='title LIKE \'%'.trim($_RQ['title']).'%\' OR keywords LIKE \'%'.trim($_RQ['title']).'%\'';
		//$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=$_RQ['pcate'];
    }
    if(!empty($_RQ['saletype'])){
        $params['saletype like']='%'.trim($_RQ['saletype']).'%';
        $return['params']['saletype']=$_RQ['saletype'];
    }
	if(!empty($_RQ['id'])){
        $params['id']=intval($_RQ['id']);
        $return['params']['id']=$_RQ['id'];
    }
    if(!empty($_RQ['brand'])){
        $params['brandid']=intval($_RQ['brand']);
        $return['params']['brand']=$_RQ['brand'];
    }
    if(!empty($_RQ['label'])){
        $params['labelname like']='%;'.trim($_RQ['label']).';%';
        $return['params']['label']=$_RQ['label'];
    }
	if(!empty($_RQ['goodssn'])){
		$return['params']['goodssn']=$_RQ['goodssn'];
		$goodsids1=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodssn like'=>'%'.trim($_RQ['goodssn']).'%'),'goodsid','goodsid');
		if($goodsids1){
			$params2=$params;
			$params2['goodssn like']='%'.trim($_RQ['goodssn']).'%';
			$goodsids2=pdo_getall('jy_weishop_goods',$params2,'id');
			if($goodsids2){
				foreach($goodsids2 as $gv){
					$goodsids1[$gv['id']]=$gv;
				}
			}
			$goodsids=array_keys($goodsids1);
			$params['id']=$goodsids;
		}else{
			$params['goodssn like']='%'.trim($_RQ['goodssn']).'%';
		}
	}
	$tip='【温馨提示】：所属商户如果为“无”，表示商户已经不存在！排序，越大越靠前！<red>商品价格解释：如果门店设置了商品价格，将以门店价格为准！规格的库存，价格也是以门店的为准！</red>';
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			pdo_update('jy_weishop_goods',array('onepluginname'=>$onepluginname),array('pluginname'=>$onepluginname,'onepluginname'=>'','uniacid'=>$_SESSION['uniacid']));
			$params['onepluginname OR']='merchid > \'0\' or onepluginname=\''.$onepluginname.'\'';
			$return['params']['onepluginname']=$onepluginname;
			$tip .='</br><red>【注意】：如果本场景已开启独立商品展示，将只显示本场景下的商品(多商户的商品除外)！</red>';
	}else{
			$params['onepluginname']='';
			$tip .='</br><red>【注意】：如果本场景已开启独立商品展示，将剔除其他独立场景的商品！</red>';
	}
	$install = check_permission('jy_poster');
	if(!$install){
		$tip .='</br><red>【注意】：还未安装《活动海报》！商品详情不显示生成海报按钮！</red>';
	}
	$tip .='</br>【<red>一键更新商品权限</red>】：是指新版多选权限格式的更新（之前是单选）</br>[温馨提示]：设为公共分类后，该分类其他场景共享！';
	if($_SESSION['isfounder']){
		$tip .='</br>【<red>打开图片有点慢？</red>】：最好设置一下附件设置，启用图片压缩！避免某些图片过大！操作教程：系统管理--系统设置--附件设置--启用图片压缩，压缩后图片宽度(本信息管理员可见)！';
	}
	$pc_set=$this->plugin_setting('pc_set');
	$substrlength=20;
	if($pc_set['is_en']==1){
		$tip='';
		$substrlength=20;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
    $fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','scate','price','status','createtime','total','salenum','unit','saletype','brandid','labelname');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,$substrlength);
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate']),'title');				
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
				$item['salenum2']=$this->pdo_getsum('order_goods',array('goodsid'=>$item['id']),'total')?:0;
				if($jy_merch){
					$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
				}
				if(!$merch['merchname'] && $item['merchid']){
					$merch['merchname']='商户不存在';
					pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				}
				$item['merchlogo']=$merch['logo'];
				$item['salecate']=$merch['salecate']?:'';
				if($pc_set['is_en']==1){
					$shopset=$this->plugin_setting('shopset');
					$item['merchname']=$merch['merchname']?:$shopset['title'];
					$item['salecate']=$merch['salecate']?:'NO';
					$item['salenum2']=$item['salenum']?:$item['salenum2'];
					$item['pcatetitle']=$item['pcatetitle']?:$shopset['title'];
					$item['ccatetitle']=$item['ccatetitle']?:'NO';
					$item['scatetitle']=$item['scatetitle']?:'NO';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/A';
					$item['oneplugintitle']='oneplugintitle';
				}else{
                    $item['oneplugintitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'商城';
					$item['merchname']=$merch['merchname']?:'平台自营';
					$item['salecate']=$merch['salecate']?:'无';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
					$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
					$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
					$item['scatetitle']=$item['scatetitle']?:'无';
				}
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$_SESSION['page']=$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
            array('type'=>'img_string','title'=>'商品ID_Goods ID',
            'data'=>array(
                array('title'=>'编号：_ID：','name'=>'id'),
                array('title'=>'排序：_Sorting：','name'=>'paixu'),
                )
            ),
			array('type'=>'img_string','title'=>'商品名称_Goods title','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'标签：_Label：','name'=>'labelname'),
				)
			),
			array('type'=>'img_string','title'=>'商品分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
            array('type'=>'img_string','title'=>'所属商户_Merch title',
            'data'=>array(
                array('title'=>'名称：_Merch：','name'=>'merchname'),
                array('title'=>'项目：_Cate：','name'=>'salecate'),
                array('title'=>'场景：_Legao：','name'=>'oneplugintitle'),
                )
            ),
			array('type'=>'img_string','title'=>'库存/销量_Inventory/Sales',
			'data'=>array(
				array('title'=>'库存：_Inventory：','name'=>'total'),
				array('title'=>'销量：_Sales：','name'=>'salenum2','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'价格(点击编辑↓)_Price','type'=>'string','set'=>'set'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'saletype','title'=>'商品属性_attribute','type'=>'checkbox','options'=>'saletypes'),
			array('name'=>'url','title'=>'复制链接_Copy Link','type'=>'copy'),
		),'action'=>array(
			array('type'=>'href','action'=>'activity.buyagain.list','title'=>'设置活动_Set activity'),
			array('type'=>'post','op'=>'copy','title'=>'复制商品_Copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus0/','title'=>'一键下架_Shelves'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus2/','title'=>'一键售罄_Sold out'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus3/','title'=>'一键加入回收站_Recycle'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/upgoods/','title'=>'一键更新商品权限_Upgoods'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/upseven/','title'=>'一键支持退款_Uptask'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/uplegao/','title'=>'设为公共商品_Uplegao'),
			array('type'=>'modal','url'=>'jy_weishop/goods.list/uppirce/','title'=>'批量修改价格'),
			array('type'=>'modal','url'=>'jy_weishop/goods.list/uppcate/','title'=>'批量修改分类'),
			array('type'=>'modal','url'=>'jy_weishop/goods.list/uptotal/','title'=>'批量修改库存'),
			array('type'=>'modal','url'=>'jy_weishop/goods.list/setprice/','title'=>'定价策略'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'type'=>'string','title'=>'商品名称_Goods'),
			array('name'=>'goodssn','width'=>10,'type'=>'string','title'=>'商品编码_Goodssn'),
			array('name'=>'id','width'=>10,'type'=>'string','title'=>'商品ID_Goods'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类_Groups','options'=>'groups'),
			array('name'=>'saletype','width'=>10,'type'=>'radio','title'=>'属性_Saletype','options'=>'saletypes'),
            array('name'=>'brand','width'=>10,'type'=>'select','title'=>'品牌_Brand','options'=>'brands'),
            array('name'=>'label','width'=>10,'type'=>'select','title'=>'标签_Label','options'=>'labels','option_value'=>'title'),
		)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$groupspram['onepluginname']=$onepluginname;
			$return['params']['onepluginname']=$onepluginname;
	}else{
			$groupspram['onepluginname']='';
	}
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id','paixu desc');
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
    $return['brands']=pdo_getall('jy_weishop_goods_brand',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
    $labels=pdo_getall('jy_weishop_goods_label',array('uniacid'=>$_SESSION['uniacid']),array('labelname'));
	$return['labels']=array();
	foreach($labels as $lv){
		$lv['labelname']=json_decode($lv['labelname'],true);
		foreach($lv['labelname'] as $lv2){
			$return['labels'][]=$lv2;
		}
	}
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs['0']=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	$return['merchs']=$merchs;
	$shopset=$this->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='otherlist'){
	$params=array('type <>'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']='1';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if(!empty($_RQ['id'])){
        $params['id']=intval($_RQ['id']);
        $return['params']['id']=$_RQ['id'];
    }
	$tip='【温馨提示】：所属商户如果为“无”，表示商户已经不存在！排序，越大越靠前！<red>商品价格解释：如果门店设置了商品价格，将以门店价格为准！规格的库存，价格也是以门店的为准！</red>';
	$tip .='</br><red>【注意】：这里显示其他场景的商品！</red>';
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['onepluginname <>']=$onepluginname;
			$return['params']['onepluginname <>']=$onepluginname;
	}else{
			$params['onepluginname <>']='';
	}
	$pc_set=$this->plugin_setting('pc_set');
	$substrlength=13;
	if($pc_set['is_en']==1){
		$tip='';
		$substrlength=20;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,$substrlength);
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate']),'title');					
				$item['url']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
				$item['salenum2']=$this->pdo_getsum('order_goods',array('goodsid'=>$item['id']),'total')?:0;
				if($jy_merch){
					$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
				}
				$item['merchname']=$merch['merchname']?:'';
				$item['merchlogo']=$merch['logo'];
				$item['salecate']=$merch['salecate']?:'';
				if($pc_set['is_en']==1){
					$shopset=$this->plugin_setting('shopset');
					$item['merchname']=$merch['merchname']?:$shopset['title'];
					$item['salecate']=$merch['salecate']?:'NO';
					$item['salenum2']=$item['salenum']?:$item['salenum2'];
					$item['pcatetitle']=$item['pcatetitle']?:$shopset['title'];
					$item['ccatetitle']=$item['ccatetitle']?:'NO';
					$item['scatetitle']=$item['scatetitle']?:'NO';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/A';
					$item['oneplugintitle']='oneplugintitle';
				}else{
					$item['oneplugintitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'电商客';
					$item['merchname']=$merch['merchname']?:'平台自营';
					$item['salecate']=$merch['salecate']?:'无';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
					$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
					$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
					$item['scatetitle']=$item['scatetitle']?:'无';
				}
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序_The sorting','type'=>'string'),
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('type'=>'img_string','title'=>'所属商户_Merch title',
			'data'=>array(
				array('title'=>'名称：_Merch：','name'=>'merchname'),
				array('title'=>'项目：_Cate：','name'=>'salecate'),
				array('title'=>'场景：_Legao：','name'=>'oneplugintitle'),
				)
			),
			array('type'=>'img_string','title'=>'商品名称_Goods title','img'=>'image',
			'data'=>array(
					array('title'=>'','name'=>'title'),
				)
			),
			array('type'=>'img_string','title'=>'商品分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量_Inventory/Sales',
			'data'=>array(
				array('title'=>'库存：_Inventory：','name'=>'total'),
				array('title'=>'销量：_Sales：','name'=>'salenum2','unit'=>'unit')
				)
			),
			array('name'=>'oneplugintitle','title'=>'所属场景','type'=>'string'),
			array('name'=>'url','title'=>'复制链接_Copy Link','type'=>'copy'),
		),'action'=>array(
			array('type'=>'href','action'=>'activity.buyagain.list','title'=>'设置活动_Set activity'),
			array('type'=>'post','op'=>'copy','title'=>'复制商品_Copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus0/','title'=>'一键下架_Shelves'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus2/','title'=>'一键售罄_Sold out'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus3/','title'=>'一键加入回收站_Recycle'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/upgoods/','title'=>'一键更新商品权限_Upgoods'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/upseven/','title'=>'一键支持退款_Uptask'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/uplegao/','title'=>'设为公共商品_Uplegao'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'type'=>'string','title'=>'商品名称_Goods'),
			array('name'=>'id','width'=>10,'type'=>'string','title'=>'商品ID_Goods'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类_Groups','options'=>'groups'),
			array('name'=>'saletype','width'=>15,'type'=>'radio','title'=>'属性_Saletype','options'=>'saletypes'),
		)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$groupspram['onepluginname']=$onepluginname;
			$return['params']['onepluginname']=$onepluginname;
	}else{
			$groupspram['onepluginname']='';
	}
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs['0']=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	$return['merchs']=$merchs;
	$shopset=$this->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=round($post['price'],2);
	}
	if($update){
		$this->pdo_update('goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	$return=model('jy_weishop/setgoods')->edit($id);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//print_r($postdata['commissions']);exit;
	$data=model('jy_weishop/setgoods')->postdata($postdata);
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin']);	
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['onepluginname']=$onepluginname?:'';
		$data['pluginname']=$onepluginname?:'';
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	model('jy_weishop/setgoods')->postup($postdata,$goodsid);
	exi('操作成功！','','goback');
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
if($_RQ['op']=='setstatus2'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('售罄成功！');
}
if($_RQ['op']=='setstatus3'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('回收成功！');
}
if($_RQ['op']=='upseven'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('seven'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('设置成功！');
}
if($_RQ['op']=='uplegao'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('onepluginname'=>''),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('回收成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upgoods'){
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','showlevels','buylevels','showgroups','buygroups'));
	foreach($goods as &$item){
		if($item['showlevels']){
			$fastvalue=substr($item['showlevels'],0,1);
			if($fastvalue!=';'){
				$item['showlevels']=';'.$item['showlevels'];
			}
			$lastvalue=substr($item['showlevels'],-1,1);
			if($lastvalue!=';'){
				$item['showlevels']=$item['showlevels'].';';
			}
		}else{
			$item['showlevels']='';
		}
		if($item['buylevels']){
			$fastvalue=substr($item['buylevels'],0,1);
			if($fastvalue!=';'){
				$item['buylevels']=';'.$item['buylevels'];
			}
			$lastvalue=substr($item['buylevels'],-1,1);
			if($lastvalue!=';'){
				$item['buylevels']=$item['buylevels'].';';
			}
		}else{
			$item['buylevels']='';
		}
		if($item['showgroups']){
			$fastvalue=substr($item['showgroups'],0,1);
			if($fastvalue!=';'){
				$item['showgroups']=';'.$item['showgroups'];
			}
			$lastvalue=substr($item['showgroups'],-1,1);
			if($lastvalue!=';'){
				$item['showgroups']=$item['showgroups'].';';
			}
		}else{
			$item['showgroups']='';
		}
		if($item['buygroups']){
			$fastvalue=substr($item['buygroups'],0,1);
			if($fastvalue!=';'){
				$item['buygroups']=';'.$item['buygroups'];
			}
			$lastvalue=substr($item['buygroups'],-1,1);
			if($lastvalue!=';'){
				$item['buygroups']=$item['buygroups'].';';
			}
		}else{
			$item['buygroups']='';
		}
		pdo_update('jy_weishop_goods',array('showlevels'=>trim($item['showlevels']),'buylevels'=>trim($item['buylevels']),'showgroups'=>trim($item['showgroups']),'buygroups'=>trim($item['buygroups'])),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('一键更新商品成功！');
}
if($_RQ['op']=='uponepluginname'){
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','showlevels','buylevels','showgroups','buygroups'));
	foreach($goods as &$item){
		
	}
	exi('一键更新商品成功！');
}
if($_RQ['op']=='copy'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择商品！','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	unset($goods['id']);
	pdo_insert('jy_weishop_goods',$goods);
	$newgoodsid=pdo_insertid();
	$specs=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(count($specs)){
		foreach($specs as $sk=>&$sv){
			$options=array();
			$spec_item=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sv['id'],'goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
			unset($sv['id']);
			$sv['goodsid']=$newgoodsid;
			pdo_insert('jy_weishop_goods_spec',$sv);
			$sv['newid']=pdo_insertid();
			if(count($spec_item)){
				foreach($spec_item as $ok=>$ov){
					$spec_item=$ov;
					unset($ov['id']);
					$ov['goodsid']=$newgoodsid;
					$ov['specid']=$sv['newid'];
					pdo_insert('jy_weishop_goods_spec_item',$ov);
					$spec_item['newid']=pdo_insertid();
					$options[]=$spec_item;
				}
			}
			$sv['options']=$options;
		}
	}
	$option=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(count($option)){
		foreach($option as $ov){
			$oldspecs=explode('_',$ov['specs']);
			$newspecs=array();
			foreach($specs as $sk=>$sp){
					foreach($sp['options'] as $op){
						if($oldspecs[$sk] == $op['id']){
							$newspecs[]=$op['newid'];
							break;
						}
					}
			}
			$ov['specs']=implode('_',$newspecs);
			$ov['goodsid']=$newgoodsid;
			unset($ov['id']);
			pdo_insert('jy_weishop_goods_option',$ov);
		}
	}
	exi('复制商品成功！');
}
if($_RQ['op']=='uppirce'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['fee']){
			exi('变动幅度不能为空','error');
		}
		$ids=$post['ids'];
		if(count($ids)==0){
			exi('请先选择商品','error');
		}
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				$price=$goods['price'];
				if($post['type']==1){
					$price=$price-$post['fee'];
				}elseif($post['type']==2){
					$price=$price*$post['fee'];
				}elseif($post['type']==3){
					$price=$price/$post['fee'];
				}else{
					$price=$price+$post['fee'];
				}				
				if($goods['costprice'] && $goods['costprice']>$price){
					exi($goods['title'].'-商品改价低于成本价！','error');
				}
				$data=array(
					'price'=>$price
				);
				pdo_update('jy_weishop_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
				if($option_list){
					foreach($option_list as &$i){
						$price=$i['price'];
						if($post['type']==1){
							$price=$price-$post['fee'];
						}elseif($post['type']==2){
							$price=$price*$post['fee'];
						}elseif($post['type']==3){
							$price=$price/$post['fee'];
						}else{
							$price=$price+$post['fee'];
						}				
						if($i['costprice'] && $i['costprice']>$price){
							exi($goods['title'].'-商品[某规格]，改价低于成本价！','error');
						}
						$data=array(
							'price'=>$price
						);
						pdo_update('jy_weishop_goods_option',$data,array('id'=>$i['id'],'uniacid'=>$_SESSION['uniacid']));
					}
					$item['specs']['tds']=$option_list;
				}
				$insert=array(
						'uniacid'=>$_SESSION['uniacid'],
						'type'=>$post['type'],
						'fee'=>$post['fee'],
						'createtime'=>TIMESTAMP
				);
				pdo_insert('jy_weishop_upprice_log',$insert);
			}
		}
		exi('修改成功！！','','/goods.list//page='.$_SESSION['page']);
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	$return['item']=array('ids'=>$ids);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('name'=>'type','title'=>'修改方向','type'=>'radio','options'=>'types'),
				array('name'=>'fee','title'=>'变动幅度','type'=>'string'),
				
		)
	);
	$return['types']=array('增加','减少','乘','除');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setprice'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('setprice',$post);
		exi('设置成功！！');
	}
	$return['item']=$this->plugin_setting('setprice');
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('type'=>'tip','title'=>'商品首次填写成本价后：会按下面的策略生成自定义会员等级的价格！如果会员等级的价格已经存在，修改成本价不会更新！'),
				array('type'=>'switch','name'=>'isint','title'=>'价格取整？'),
				array('type'=>'array','title'=>'价格策略','name'=>'set','add_tip'=>'添加一组价格策略',
					'list'=>array(
							array('type'=>'select','name'=>'levelid','title'=>'所属会员等级','options'=>'levels2'),
							array('type'=>'unitstring','unit'=>'元','name'=>'minprice','title'=>'下限'),
							array('type'=>'unitstring','unit'=>'元','name'=>'maxprice','title'=>'上限'),
							array('type'=>'unitstring','unit'=>'%','name'=>'pricerate','title'=>'加价%'),
							
					),
					'details'=>array(
							array('type'=>'select','name'=>'levelid','title'=>'所属会员等级','options'=>'levels2','help'=>'无会员等级，请先移步会员-会员等级，添加！'),
							array('type'=>'unitstring','unit'=>'元','name'=>'minprice','title'=>'下限'),
							array('type'=>'unitstring','unit'=>'元','name'=>'maxprice','title'=>'上限'),
							array('type'=>'unitstring','unit'=>'%','name'=>'pricerate','title'=>'加价%','help'=>'必须是数字，+成本价%'),
					)
			),
		)
	);
	$levels=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels2']=$levels;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='editprice'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_weishop_goods',array('price'=>$post['price']),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		$options=$post['options'];
		foreach($options as $ov){
			if($ov['id']){
				pdo_update('jy_weishop_goods_option',array('price'=>$ov['price'],'costprice'=>$ov['costprice']),array('id'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		exi('设置成功！！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','price','costprice','title'));
	$options=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
	foreach($options as &$ov){
			$title='';
			$ov['specs']=explode('_',$ov['specs']);
			foreach($ov['specs'] as $j){
				$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('id','specid','title'));
				$title .=' '.$op['title'];
			}
			$ov['title']=$title;
	}
	$goods['options']=$options;
	$return['item']=$goods;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('type'=>'string','name'=>'price','title'=>'商品价格'),
				
		)
	);
	if(count($options)){
		$htmls['edit'][]=array('type'=>'array_simple','title'=>'规格','name'=>'options','noadd'=>1,'nosplice'=>1,'nodel'=>1,
					'fields'=>array(
							array('type'=>'string','name'=>'title','read'=>1,'title'=>'规格'),
							array('type'=>'string','name'=>'price','title'=>'销售价'),
							array('type'=>'string','name'=>'costprice','title'=>'成本价'),
					),
		);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='uppcate'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$ids=$post['ids'];
		if(count($ids)==0){
			exi('请先选择商品','error');
		}
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$update=array('pcate'=>intval($post['pcate']),'ccate'=>intval($post['ccate']),'scate'=>intval($post['scate']));
				pdo_update('jy_weishop_goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		exi('修改成功！！');
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	$return['item']=array('ids'=>$ids);
	$shopset=$this->plugin_setting('shopset');
	$catelevel=$shopset['level']?3:2;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类','help'=>'[温馨提示]：如果场景下，开启了独立商品，此处只显示本场景下的分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'请选择二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'请选择三级分类',
				),
				
		)
	);
	//获取分类
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'parentid'=>0);
	$onepluginname=model('jy_weishop/goods')->is_onegoods($_SESSION['current_plugin']);
	if($onepluginname){//开启独立商品
		$groupspram['onepluginname']=$onepluginname;
		$return['params']['onepluginname']=$onepluginname;
	}else{
		$groupspram['onepluginname']='';
	}
	$return['pcates']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='uptotal'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$ids=$post['ids'];
		if(count($ids)==0){
			exi('请先选择商品','error');
		}
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$update=array('total'=>intval($post['total']));
				pdo_update('jy_weishop_goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		exi('修改成功！！');
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	$return['item']=array('ids'=>$ids);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('name'=>'total','title'=>'库存','type'=>'string','help'=>'<red>明明有库存，但是前端显示没库存?有可能前端指定了门店，该门店下该商品没有库存！</red>;</br><red>注意：两种情况库存不准</red>1、如果已经添加了多规格的，库存将自动计算规格库存的总和！2、你设置的卡密，但是卡密里面没有卡密数据了！'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}